<template>
  <div>
    <el-row>
      <el-dialog v-dialogDrag :visible.sync="visible" :title="title" center :width="width">
        <el-tree
          :data="menu"
          show-checkbox
          default-expand-all
          node-key="uuid"
          ref="tree"
          @check="itemClick"
          :check-strictly="true"
          highlight-current
          :current-node-key="key"
          :props="defaultProps">
        </el-tree>
<!--        <div class="buttons">-->
<!--          <el-button @click="getCheckedKeys">通过 key 获取</el-button>-->
<!--          <el-button @click="setCheckedKeys">通过 key 设置</el-button>-->
<!--        </div>-->
        <span class="dialog-footer" slot="footer" style="text-align: right!important;">
          <el-button size="mini" @click="visible = false">取 消</el-button>
          <el-button size="mini" @click="save" type="primary">保 存</el-button>
        </span>
      </el-dialog>

    </el-row>
  </div>
</template>

<script>
  import {ROLE, SYSTEM} from '@/api'
  import {mapGetters} from 'vuex'

  export default {
    components: {},
    props: {
      show: {Boolean, default: false},
      value: {Object, default: {}},
      width: {String, default: "50%"},
      title: {String, default: "权限"},
      type: {Number, default: 0} // 0 新增 1 查看
    },
    data() {
      return {
        form: {},
        key: '',
        menu: [],
        defaultProps: {
          children: 'children',
          label: 'title'
        }
      }
    },
    computed: {
      visible: {
        get() {
          return this.show
        },
        set(val) {
          this.$emit("update:show", false)
        }
      }
    },
    mounted() {
      this.setOldData(this.value)
      this.getRoles()
    },
    watch: {},
    methods: {
      setOldData(data) {
        this.getMenuList()
        this.form = this.$newObject(data)
      },
      getMenuList() {
        SYSTEM.menuList().then(res => {
          if (res.code === 200) {
            this.menu = res.data
          } else {
            this.menu = []
          }
        })
      },
      getRoles() {
        const roleUuid = this.form.uuid
        ROLE.getRoleMenu({roleUuid}).then(res => {
          if (res.code === 200) {
            this.$refs.tree.setCheckedKeys([...res.data])
          }
        })
      },
      save() {
        const keys = this.$refs.tree.getCheckedKeys()
        const data = keys.map(k => {
          return {menuUuid: k}
        })
        const params = {
          roleUuid: this.form.uuid
        }
        ROLE.saveRoleMenu(data, params).then(res => {
          if (res.code === 200) {
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
          this.$emit("update:show", false)
        })
      },
      itemClick(...par) {
        const node = par[0]
        const halfChicked = par[1]
        // console.log(halfChicked.halfCheckedKeys)
        // console.log(halfChicked.checkedKeys)
      },
      getCheckedNodes() {
        console.log(this.$refs.tree.getCheckedNodes())
      },
      getCheckedKeys() {
        console.log(this.keyList)
        console.log(this.$refs.tree.getCheckedKeys())
      },
      setCheckedNodes() {
        this.$refs.tree.setCheckedNodes([{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 9,
          label: '三级 1-1-1'
        }])
      },
      setCheckedKeys() {
        this.$refs.tree.setCheckedKeys([3])
      },
      resetChecked() {
        this.$refs.tree.setCheckedKeys([])
      }
    }
  }
</script>
<style lang="scss" scoped>
  .small-card {
    ::v-deep .el-card__body {
      padding: 10px;
    }
  }

  .el-form {
    ::v-deep .el-form-item {
      margin-bottom: 10px;
    }
  }
</style>
